/**多页面配置参数 */
// let projectName = process.env.PROJECT_NAME;
const fs = require("fs");
const path = require("path");
const chalk = require("chalk");
/** 读取并自动生成配置文件 **/
var configProject = {};
var rootpath = path.resolve(__dirname, "src/projects");
var currPro = "";
function getpages(filePath, pro) {
	var pages = fs.readdirSync(filePath);
	pages.forEach((item) => {
		configProject[pro]["pages"][item] = {
			entry: path.join(`src/projects/${pro}/pages/${item}/${process.env.target ? process.env.target : "main"}.js`),
			template: path.join(`src/projects/${pro}/pages/${item}/public/index.html`),
		};
	});
}

function walkSync(currentDirPath, callback) {
	fs.readdirSync(currentDirPath).forEach(function(name) {
		var filePath = path.join(currentDirPath, name);
		var stat = fs.statSync(filePath);

		if (stat.isDirectory()) {
			if (currentDirPath == rootpath) {
				var projectName = filePath.substring(filePath.lastIndexOf(path.join("/")) + 1);
				currPro = projectName;
				configProject[projectName] = {
					pages: {},
				};
			} else {
				if (filePath.substring(filePath.lastIndexOf(path.join("/")) + 1) == "pages") {
					getpages(filePath, currPro);
					return;
				}
			}

			// console.log("执行循环")
			walkSync(filePath, callback);
		}
	});
}
walkSync(rootpath);
// return false

console.log(chalk.yellow.bold("项目配置完成：\n"));

/**生成目录结构参考config的配置项**/
// const config = {
//     projectA: {
//         pages: {
//             list: {
//                 entry: `src/projects/${projectName}/pages/list/main.js`,
//                 template: `src/projects/${projectName}/pages/list/public/index.html`,
//                 title: "电子合同",
//                 // filename: "list.html",
//                 // chunks: ['chunk-vendors', 'chunk-common', 'index']
//             },
//             detail: {
//                 entry: `src/projects/${projectName}/pages/detail/main.js`,
//                 template: `src/projects/${projectName}/pages/detail/public/index.html`,
//                 title: "合同详情",
//                 // chunks: ['chunk-vendors', 'chunk-common', 'index']
//             },
//         },
//     },
//     projectB: {
//         pages: {
//             list: {
//                 entry: `src/projects/${projectName}/pages/list/main.js`,
//                 template: `src/projects/${projectName}/pages/list/public/index.html`,
//                 title: "电子证明",
//                 // filename: "list.html",
//                 // chunks: ['chunk-vendors', 'chunk-common', 'index']
//             },
//             detail: {
//                 entry: `src/projects/${projectName}/pages/detail/main.js`,
//                 template: `src/projects/${projectName}/pages/detail/public/index.html`,
//                 title: "证明详情",
//                 // chunks: ['chunk-vendors', 'chunk-common', 'index']
//             },
//             createType: {
//                 entry: `src/projects/${projectName}/pages/createType/main.js`,
//                 template: `src/projects/${projectName}/pages/createType/public/index.html`,
//                 title: "证明类型",
//                 // chunks: ['chunk-vendors', 'chunk-common', 'index']
//             },
//             apply: {
//                 entry: `src/projects/${projectName}/pages/apply/main.js`,
//                 template: `src/projects/${projectName}/pages/apply/public/index.html`,
//                 title: "证明申请",
//                 // chunks: ['chunk-vendors', 'chunk-common', 'index']
//             }
//         },
//     },
//     projectC: {
//         pages: {
//             index: {
//                 entry: `src/projects/${projectName}/pages/index/main.js`,
//                 template: `src/projects/${projectName}/pages/index/public/index.html`,
//                 title: "电子签",
//             },
//         },
//     },
// };

module.exports = configProject;
